/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fgv.tcc.fgv.imovel.service;

import br.com.fgv.tcc.fgv.imovel.dao.ConfiguracaoDao;
import br.com.fgv.tcc.fgv.imovel.dao.Dao;
import br.com.fgv.tcc.fgv.imovel.dao.ImagemDao;
import br.com.fgv.tcc.fgv.imovel.dao.ImovelDao;
import br.com.fgv.tcc.fgv.imovel.dao.SituacaoDao;
import br.com.fgv.tcc.fgv.imovel.model.Configuracao;
import br.com.fgv.tcc.fgv.imovel.model.Imagem;
import br.com.fgv.tcc.fgv.imovel.model.Imovel;
import br.com.fgv.tcc.fgv.imovel.model.Situacao;
import br.com.fgv.tcc.fgv.imovel.model.Usuario;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

/**
 *
 * @author adao
 */
@Stateless
@Path("imovel")
public class ImovelService extends ServiceGeneric {

    private ImovelDao imovelDao;

    private ConfiguracaoDao configuracaoDao;
    
    private String baseUploa;

    private String getBaseUpload() {
        if (baseUploa == null) {
            c = (Configuracao) configuracaoDao.recuperar(1);
            baseUploa = c.getValor();
        }
        return baseUploa;
    }
    Configuracao c = new Configuracao();

    public ImovelService() {
        super(Imovel.class);
        imovelDao = new ImovelDao();
        configuracaoDao = new ConfiguracaoDao();

    }

    @Override
    protected Dao getDaoManager() {
        return imovelDao;
    }

    @GET
    @Path("recuperaImovel/{id}")
    @Produces("application/json")
    public Usuario recuperaImovel(@PathParam("id") int id) {
        return (Usuario) super.recuperar(id);
    }

    @GET
    @Path("listarTodosImoveis")
    @Produces("application/json")
    public List<Usuario> listarTodosImoveis() {
        return super.listarTodos(); //To change body of generated methods, choose Tools | Templates.
    }

    SituacaoDao situacaoDao = new SituacaoDao();
    ImagemDao imagemDao = new ImagemDao();

    @POST
    @Path("uploadImovel")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public String uploadArquivo(
            @FormDataParam("imagem") InputStream fileInputStream,
            @FormDataParam("imagem") FormDataContentDisposition fileDetail,
            @FormDataParam("titulo") String titulo,
            @FormDataParam("descricao") String descricao)
            throws FileNotFoundException, IOException {

        String fileName = System.currentTimeMillis() + "_" + fileDetail.getFileName();
        String filePath = getBaseUpload() + fileName;
        try (OutputStream fileOutputStream = new FileOutputStream(filePath)) {
            int read = 0;
            final byte[] bytes = new byte[1024];
            while ((read = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, read);
            }
            fileOutputStream.close();
        }
        Imagem imagem = new Imagem();
        imagem.setCaminho("upload/" + fileName);

        Imovel imovel = new Imovel();
        imovel.setTitulo(titulo);
        imovel.setDescricao(descricao);

        imovel.getListaImagens().add(imagem);
        imovel.setSituacao((Situacao) situacaoDao.recuperar(1));

        super.salvar(imovel);

        return "ok";
    }

}
